﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace Global
{
    /// <summary>
    /// 
    /// </summary>
    public static class PlatformUtility
    {
        private static WinGameInput _winGameInput = new WinGameInput();
        private static SingleTouchGameInput _singleTouchGameInput = new SingleTouchGameInput();
        public static bool IsTouchDevice
        {
            get
            {
                return Application.platform == RuntimePlatform.Android ||
                       Application.platform == RuntimePlatform.IPhonePlayer;
            }
        }

        public static IGameInput Input
        {
            get
            {
                if (IsTouchDevice)
                {
                    return _winGameInput;
                }
                else
                {
                    return _singleTouchGameInput;
                }
            }
        }
    }

    public interface IGameInput
    {
        /// <summary>
        /// 是否为触摸设备
        /// </summary>
        bool IsTouchDevice { get; }
       /// <summary>
       /// 是否点击按下
       /// </summary>
        bool IsClickDown { get; }
        /// <summary>
        /// 是否点击弹起
        /// </summary>
        bool IsClickUp { get; }
        /// <summary>
        /// 是否点击按住
        /// </summary>
        bool IsClicking { get; }
        /// <summary>
        /// 是否有触摸
        /// </summary>
        bool HasTouch { get; }
        /// <summary>
        /// 是否触摸移动
        /// </summary>
        bool IsMove { get; }
        /// <summary>
        /// 光标位置
        /// </summary>
        Vector3 MousePosition { get; }
        /// <summary>
        /// 触摸点数量
        /// </summary>
        int TouchCount { get; }
    }

    internal class WinGameInput : IGameInput
    {
        /// <summary>
        /// 地图边界左边
        /// </summary>
        public static float EdgeLeftX = 20.2f;
        /// <summary>
        /// 地图边界右边
        /// </summary>
        public static float EdgeRightX = 38.3f;
        /// <summary>
        /// 地图边界下边
        /// </summary>
        public static float EdgeDownY = 7.6f;
        /// <summary>
        /// 地图边界上边
        /// </summary>
        public static float EdgeUpY = 21.6f;
        /// <summary>
        /// 相机移动速度
        /// </summary>
        public static float CameraMoveSpeed = 0.3f;
        /// <summary>
        /// 地图边界宽度
        /// </summary>
        public static float EdgeWindth = 0.5f;
        public bool IsClickDown
        {
            get { return Input.GetMouseButtonDown(0); }
        }

        public bool IsClickUp
        {
            get { return Input.GetMouseButtonUp(0); }
        }

        public bool IsClicking
        {
            get { return Input.GetMouseButton(0); }
        }

        public bool HasTouch
        {
            get { return true; }
        }

        public bool IsMove
        {
            get { return IsClicking; }
        }

        public Vector3 MousePosition
        {
            get { return Input.mousePosition; }
        }

        public int TouchCount
        {
            get { return 1; }
        }

        public bool IsTouchDevice
        {
            get { return PlatformUtility.IsTouchDevice; }
        }
    }
    internal class SingleTouchGameInput : IGameInput
    {
        public bool IsTouchDevice
        {
            get { return PlatformUtility.IsTouchDevice; }
        }
        public bool HasTouch
        {
            get { return Input.touchCount > 0; }
        }

        public bool IsClickDown
        {
            get
            {
                return Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Began;
            }
        }

        public bool IsClicking
        {
            get
            {
                return Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Stationary;
            }
        }

        public bool IsClickUp
        {
            get
            {
                return Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Ended;
            }
        }

        public bool IsMove
        {
            get
            {
                return Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved;
            }
        }

        public Vector3 MousePosition
        {
            get
            {
                if (Input.touchCount == 1)
                {
                    return Input.GetTouch(0).position;
                }
                else
                {
                    return Input.mousePosition;
                }
            }
        }

        public int TouchCount
        {
            get { return Input.touchCount; }
        }
    }
}